package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.mongo.RecommendUser;
import com.tanhua.mongo.UserLike;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class
RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;    //操作mongoDB


    /**
     * 推荐今日佳人逻辑
     *
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser RecommendMaxScore(Long toUserId) {
        //构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象,降序排序后拿分数最高的
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        //使用mongoTemplate查询
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 推荐好友列表
     *
     * @param page
     * @param pagesize
     * @param toUserId
     * @return
     */
    @Override
    public PageResult recommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria);
        //查询总页数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //查询满足条件的list
        query.with(Sort.by(Sort.Order.desc("score")))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        //封装返回PageResult
        return new PageResult(page, pagesize, count, recommendUsers);
    }

    /**
     * 通过推荐人id和被推荐人id查询recommend
     *
     * @param userId
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryRecommend(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("toUserId").is(toUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        //如果无recommendUser为null，自动设置缘分值为80
        if(recommendUser == null){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            //设置默认缘分值
            recommendUser.setScore(80D);
        }
        return recommendUser;
    }

    /**
     * 查询推荐人卡片，要排除已喜欢或不喜欢的
     * @param userId
     * @return
     */
    @Override
    public List<RecommendUser> queryRecommendList(Long userId) {
        //查询已喜欢或不喜欢的
        List<UserLike> userLikes = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> ids = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        //构造查询推荐用户的条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("toUserId").nin(ids);
        //构造统计条件
        TypedAggregation<RecommendUser> aggregation = TypedAggregation.newAggregation(
                //操作数据类型
                RecommendUser.class,
                //指定查询条件
                Aggregation.match(criteria),
                //统计的函数(默认查询10个推荐卡片)
                Aggregation.sample(10)
        );
        //第一个参数为统计条件，第二个为返回值类型
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        //results中包含统计的数据和统计详情
        return results.getMappedResults();
    }
}
